﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InfoPick.Lib
{
    public class EGInfo
    {
        public static List<EGInfo> Load(string file)
        {
            int tmpId = 0;
            bool serving = false;
            bool target = false;
            string sCell = "";
            string tCell = "";
            List<EGInfo> lists = new List<EGInfo>();
            EGInfo tmpEGInfo = new EGInfo();

            StringMath math = new StringMath();

            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            var gb18030 = Encoding.GetEncoding("GB18030");

            StreamReader sr = new StreamReader(fs, gb18030);

            //使用StreamReader类来读取文件 
            while (!sr.EndOfStream)
            {
                string strReadLine = sr.ReadLine(); //读取每行数据             

                if (strReadLine.Contains("序号"))
                {
                    string returnId = math.FindstrBackSign(strReadLine,"]");
                    int intId =  Convert.ToInt32(returnId);
                    
                    if(tmpId == 0)
                    {
                        tmpId = Convert.ToInt32(returnId); 
                    }else
                    {
                        if (tmpId != intId)
                        {
                            lists.Add(tmpEGInfo);
                            EGInfo tmpEGInfo2 = new EGInfo();
                            tmpEGInfo = tmpEGInfo2;
                        }
                    }
                    tmpEGInfo.Id = intId;
                    
                }
                if (strReadLine.Contains("时间"))
                {
                    string returnTime = math.FindstrBackSign(strReadLine,"]");
                    tmpEGInfo.Time = DateTime.Parse(returnTime);
                }
                if (strReadLine.Contains("消息类型"))
                {
                    string returnMessageType = math.FindstrBackSign(strReadLine,"]");
                    tmpEGInfo.MessageType = returnMessageType;
                }
                if (strReadLine.Contains("ie-dialogue-id:"))
                {
                    string returnIeDialogueId = math.FindstrBetween(strReadLine, "(", ")");
                    tmpEGInfo.IeDialogueId = Convert.ToInt32(returnIeDialogueId);
                }
                if (strReadLine.Contains("cell-identifier-serving"))
                {
                    serving = true;
                }
                if (serving && !strReadLine.Contains("mnc-dig-3") && (strReadLine.Contains("c-dig-") || strReadLine.Contains("lac:"))
                    && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    string tmpStr = math.FindstrBetween(strReadLine, "(", ")");
                    if (strReadLine.Contains("lac:"))
                    {
                        tmpStr = math.FindstrBetween(strReadLine, "x", "(");
                    }
                    sCell += tmpStr;
                }
                if (serving && strReadLine.Contains("ci-value:") && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    serving = false;
                    string tmpStr = math.FindstrBetween(strReadLine, "x", "(");
                    sCell += tmpStr;
                    tmpEGInfo.OrigDistrict = sCell;
                    sCell = "";
                }
                if (strReadLine.Contains("cell-identifier-target") && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    target = true;
                }
                if (target && !strReadLine.Contains("mnc-dig-3") && (strReadLine.Contains("c-dig-") || strReadLine.Contains("lac:"))
                    && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    string tmpStr = math.FindstrBetween(strReadLine, "(", ")");
                    if (strReadLine.Contains("lac:"))
                    {
                        tmpStr = math.FindstrBetween(strReadLine, "x", "(");
                    }
                    tCell += tmpStr;
                    tCell = tCell.Trim();
                }
                if (target && strReadLine.Contains("ci-value:") && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    target = false;
                    string tmpStr = math.FindstrBetween(strReadLine, "x", "(");
                    tCell += tmpStr;
                    tmpEGInfo.TargDistrict = tCell.Trim().ToString();
                    tCell = "";
                }
                if (strReadLine.Contains("invalid-cell") && tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_CNF"))
                {
                    tmpEGInfo.invalid_cell = true;
                }
                if (tmpEGInfo.MessageType != null)
                {
                    if (tmpEGInfo.MessageType.Contains("MAP_PREPARE_HANDOVER_CNF") && strReadLine.Contains(".....content:"))
                    {
                        string tmpStr = math.FindstrBackSign(strReadLine, ":");
                        tmpEGInfo.TargServerId = tmpStr.Substring(0, 10);
                    }
                }     
            }
            if (sr.EndOfStream)
            {
                lists.Add(tmpEGInfo);
            }
            return lists;
        }

        /// <summary>
        /// number
        /// </summary>
        public int Id { get; set; }
        
        /// <summary>
        /// ie-dialogue-id
        /// </summary>
        public int IeDialogueId { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// MessageType
        /// </summary>
        public string MessageType { get; set; }

        /// <summary>
        /// 源小区id
        /// </summary>
        public string OrigDistrictId { get; set; }

        /// <summary>
        /// 源小区
        /// </summary>
        public string OrigDistrict { get; set; }

        /// <summary>
        /// 源小区名字
        /// </summary>
        public string OrigDistrictName { get; set; }

        /// <summary>
        /// 目标server id 取前10位数字
        /// </summary>
        public string TargServerId { get; set; }

        /// <summary>
        /// 目标小区
        /// </summary>
        public string TargDistrict { get; set; }

        /// <summary>
        /// 目标小区名字
        /// </summary>
        public string TargDistrictName { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public bool invalid_cell { get; set; }
    }
}
